package com.smart.controller;

import com.smart.common.ResultVo;
import com.smart.entity.Toke;
import com.smart.entity.Users;
import com.smart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/chuiniubi")
    public ResultVo chuiniubi() {
        String result = "李阿豪，您不能不要对我吹牛逼A，吹牛逼能让你发家致富吗？";
        System.out.println("成功请求一次");
        return ResultVo.ok().data("item", result);
    }
    @RequestMapping("/login")
    public ResultVo LoginAdmin(String username, String password) {
        Toke login = userService.Login(username, password);
        return ResultVo.ok().data("item", login);
    }

    @GetMapping("/user/logout")
    public ResultVo logout() {
        userService.Logout();
        return ResultVo.ok().data("item", "退出成功！");
    }

    @PostMapping("/user/register")
    public ResultVo Register(Users user) {
        if(user.getUsername().equals("") && user.getPassword().equals("") &&  user.getRealname().equals("")){
            return ResultVo.error().data("msg","不能为空！");
        }
        userService.UserAdd(user);
        return ResultVo.ok().data("item", "退出成功！");
    }

    @RequestMapping("/logout")
    public ResultVo Logout(String username, String password) {
        System.out.println(username + password);
        Toke login = userService.Login(username, password);
        return ResultVo.ok().data("item", login);
    }

    @GetMapping("/user/perinfo/all")
    public ResultVo PerInfoAll() {
        Users users = userService.PerInfoAll();
        return ResultVo.ok().data("item", users);
    }

    @PostMapping("/user/perinfo/edit")
    public ResultVo PerInfoEdit(Users user) {
        Integer result = userService.PerInfoEdit(user);
        return ResultVo.ok().data("item", result);
    }

    @PostMapping("/user/changepwd/edit")
    public ResultVo ChangePwdEdit(String password) {
        System.out.println(password+"哈哈哈");
        Integer result = userService.ChangePwdEdit(password);
        return ResultVo.ok().data("item", result);
    }


    @GetMapping("index")
    public String index() {
        return "index";
    }

    @GetMapping("hello")
    public ResultVo hello() {
        try{
            return ResultVo.ok().data("item", "hello！");
        } catch (Exception e) {
            return ResultVo.error().data("item", "请重新登录！");
        }
    }

    @Resource
    StringRedisTemplate stringRedisTemplate;
    @GetMapping("redis/set")
    public String setValue(){
        String key = "username";
        String value = "zhangsan啊";
        stringRedisTemplate.opsForValue().set(key,value);
        return "set succ";
    }
    @GetMapping("redis/get")
    public String getValue(){
        String key = "username";
        String result = stringRedisTemplate.opsForValue().get(key);
        return result;
    }
}

